home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C & C++ Multimedia Cyber Classroom
/
C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso
/
src
/
fig18_06.jar
/
Ch18
/
Fig18_06
/
Fig18_06.cpp
Wrap
C/C++ Source or Header
|
1997-11-10
|
976b
|
49 lines
// Fig. 18.6: fig18_06.cpp
// Using signal handling
#include <iostream.h>
#include <iomanip.h>
#include <signal.h>
#include <stdlib.h>
#include <time.h>
void signal_handler( int );
int main()
{
signal( SIGINT, signal_handler );
srand( time( 0 ) );
for ( int i = 1; i < 101; i++ ) {
int x = 1 + rand() % 50;
if ( x == 25 )
raise( SIGINT );
cout << setw( 4 ) << i;
if ( i % 10 == 0 )
cout << endl;
}
return 0;
}
void signal_handler( int signalValue )
{
cout << "\nInterrupt signal (" << signalValue
<< ") received.\n"
<< "Do you wish to continue (1 = yes or 2 = no)? ";
int response;
cin >> response;
while ( response != 1 && response != 2 ) {
cout << "(1 = yes or 2 = no)? ";
cin >> response;
}
if ( response == 1 )
signal( SIGINT, signal_handler );
else
exit( EXIT_SUCCESS );
}